<div class="layuimini-main">
    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">楼宇</label>
            <div class="layui-input-inline">
                <select name="buildingId" lay-filter="buildingId" id="buildingId">
                    <option value="">请选择楼宇</option>
                </select>
            </div>
            <div class="layui-form-mid">-</div>
            <div class="layui-input-inline">
                <select name="storeyId" lay-filter="storeyId" id="storeyId">
                    <option value="">请选择楼层</option>
                </select>
            </div>
            <div class="layui-form-mid">-</div>
            <div class="layui-input-inline">
                <select name="dormitoryId" lay-filter="dormitoryId" id="dormitoryId">
                    <option value="">请选择宿舍</option>
                </select>
            </div>
            <div class="layui-form-mid">-</div>
            <div class="layui-input-inline">
                <select name="studentId" lay-filter="studentId" id="studentId">
                    <option value="">请选择学生</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">开始时间</label>
            <div class="layui-input-block">
                <input type="text" name="startTime" id="startTime" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">结束时间</label>
            <div class="layui-input-block">
                <input type="text" name="endTime" id="endTime" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注信息</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table','axios','ztree','laydate'], function () {
        let form = layui.form,
            layer = layui.layer,
            ztree = layui.ztree,
            laydate = layui.laydate,
            axios = layui.axios,
            $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */

        //日期
        laydate.render({
            elem: '#startTime'
        });
        laydate.render({
            elem: '#endTime'
        });

        // 当前弹出层，防止ID被覆盖
        let parentIndex = layer.index;
        axios.post('building/query',{}).then(function (response) {
            response.data.forEach(item=>{
                let option = `<option value="${item.id}">${item.name}</option>`
                $('#buildingId').append(option);
            })
            form.render();
        });


        form.on('select(buildingId)', function(data){
            axios.post('storey/query',{buildingId:data.value,limit:1000}).then(function (response) {
                $('#storeyId').empty();
                $('#storeyId').append('<option value="">请选择楼层</option>');
                response.data.forEach(item=>{
                    let option = `<option value="${item.id}">${item.name}</option>`
                    $('#storeyId').append(option);
                })
                form.render();
            });
        });

        form.on('select(storeyId)', function(data){
            axios.post('dormitory/query',{storeyId:data.value,limit:1000}).then(function (response) {
                $('#dormitoryId').empty();
                $('#dormitoryId').append('<option value="">请选择宿舍</option>');
                response.data.forEach(item=>{
                    let option = `<option value="${item.id}">${item.no}</option>`
                    $('#dormitoryId').append(option);
                })
                form.render();
            });
        });

        form.on('select(dormitoryId)', function(data){
            axios.post('dormitoryStudent/query',{dormitoryId:data.value,limit:1000}).then(function (response) {
                $('#studentId').empty();
                $('#studentId').append('<option value="">请选择学生</option>');
                response.data.forEach(item=>{
                    let option = `<option value="${item.student.id}">${item.student.name}</option>`
                    $('#studentId').append(option);
                })
                form.render();
            });
        });


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            axios.post('absence/create',data.field).then(function(response){
                if(response.code == 200){
                    layer.msg(response.msg);
                    window.reload();
                }
                layer.close(parentIndex);
            }).catch(function (error) {
                layer.msg(error);
            });
            return false;
        });

    });
</script>